package com._520it._06_many2many.test;


import com._520it._06_many2many.domain.Student;
import com._520it._06_many2many.domain.Teacher;
import com._520it._06_many2many.mapper.StudentMapper;
import com._520it._06_many2many.mapper.TeacherMapper;
import com._520it.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;


public class Apptest {

    @Test
    public void testSave() throws Exception {
        SqlSession session = MybatisUtil.getSession();
        TeacherMapper teacherMapper = session.getMapper(TeacherMapper.class);
        StudentMapper studentMapper = session.getMapper(StudentMapper.class);

        //建立对象,设置属性
        Teacher teacher1 = new Teacher();
        teacher1.setName("杨振宁");
        Teacher teacher2 = new Teacher();
        teacher2.setName("邓稼先");

        Student student1 = new Student();
        student1.setName("温家宝");
        Student student2 = new Student();
        student2.setName("温家宝");

        //设置对象间的关系
        teacher1.getStuList().add(student1);
        teacher1.getStuList().add(student2);
        teacher2.getStuList().add(student1);
        teacher2.getStuList().add(student2);

        //保存对象: 先保存对象, 保存完后在保存关系

        teacherMapper.save(teacher1);
        teacherMapper.save(teacher2);
        studentMapper.save(student1);
        studentMapper.save(student2);

        //根据所维护的对象的id的变化, 来保存中间表的信息
        for (Student stu : teacher1.getStuList()) {
            teacherMapper.saveRelation(teacher1.getId(), stu.getId());
        }

        for (Student stu : teacher2.getStuList()) {
            teacherMapper.saveRelation(teacher2.getId(), stu.getId());
        }

        session.commit();
        session.close();
    }


    //额外SQL
    @Test
    public void testget1() throws Exception {

        SqlSession session = MybatisUtil.getSession();
        TeacherMapper teacherMapper = session.getMapper(TeacherMapper.class);

        Teacher teacher = teacherMapper.get1(9L);
        System.out.println(teacher);

        session.commit();
        session.close();
    }


    //显式连接多表查询
    @Test
    public void testget2() throws Exception {

        SqlSession session = MybatisUtil.getSession();
        TeacherMapper teacherMapper = session.getMapper(TeacherMapper.class);

        Teacher teacher = teacherMapper.get2(9L);
        System.out.println(teacher);

        session.close();
    }

    //查询所有
    @Test
    public void testList() throws Exception {

        SqlSession session = MybatisUtil.getSession();
        TeacherMapper teacherMapper = session.getMapper(TeacherMapper.class);

        List<Teacher> teacherList = teacherMapper.list();

        for (Teacher teacher : teacherList) {
            System.out.println(teacher);
        }

        session.close();
    }


    @Test
    public void testDelete() throws Exception {

        SqlSession session = MybatisUtil.getSession();
        TeacherMapper teacherMapper = session.getMapper(TeacherMapper.class);

        teacherMapper.deleteRealtion(2L);
        teacherMapper.delete(2L);

        session.commit();
        session.close();
    }


    @Test
    public void testGet3() throws Exception {

        SqlSession session = MybatisUtil.getSession();
        StudentMapper studentMapper = session.getMapper(StudentMapper.class);
        Student student = studentMapper.get3(3L);
        System.out.println(student);
        session.commit();
        session.close();
    }
    @Test
    public void testList3() throws Exception {

        SqlSession session = MybatisUtil.getSession();
        StudentMapper studentMapper = session.getMapper(StudentMapper.class);
        List<Student> studentList = studentMapper.list3();
        for (Student student : studentList) {
            System.out.println(student);
        }
        session.commit();
        session.close();
    }


}
